// 通用函数
import useClipboard from 'vue-clipboard3';
import { storeToRefs } from 'pinia';
import { ElMessage } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import orderState from './pageState/orderState';
import { useCityList } from '/@/stores/cityList';
import { CN, GB, US, DE, SITE_GB, SITE_US, SITE_DE } from '/@/stores/cityList';

const storesCity = useCityList();
const { storeCityList } = storeToRefs(storesCity);

const {
	PAYMENT_PAYPAL,
	PAYMENT_AFTERPAY,
	PAYMENT_KLARNA,
	PAYMENT_CREDIT_CARD,
	PENDING_PAYMENT,
	TO_BE_SHIPPED,
	SHIPPED,
	COMPLETED,
	CLOSED,
	CANCELLED,
	INVALID_ORDER,
} = orderState();

export default function () {
	// 0->德国；1->英国；2->美国
	const COUNTRY_GERMANY = 0;
	const COUNTRY_BRITAIN = 1;
	const COUNTRY_USA = 2;
	const { toClipboard } = useClipboard();
	//百分比格式化
	const percentFormat = (row, column, cellValue) => {
		return cellValue ? `${cellValue}%` : '-';
	};
	//列表日期时间格式化
	const dateFormatYMD = (row, column, cellValue) => {
		if (!cellValue) return '-';
		return formatDate(new Date(cellValue), 'YYYY-mm-dd');
	};
	//列表日期时间格式化
	const dateFormatYMDHMS = (row, column, cellValue) => {
		if (!cellValue) return '-';
		return formatDate(new Date(cellValue), 'YYYY-mm-dd HH:MM:SS');
	};
	//列表日期时间格式化
	const dateFormatHMS = (row, column, cellValue) => {
		if (!cellValue) return '-';
		let time = 0;
		if (typeof row === 'number') time = row;
		if (typeof cellValue === 'number') time = cellValue;
		return formatDate(new Date(time * 1000), 'HH:MM:SS');
	};
	// 小数格式化
	const scaleFormat = (value = '0', scale = 4) => {
		return Number.parseFloat(value).toFixed(scale);
	};
	// 小数格式化
	const scale2Format = (value = '0') => {
		return Number.parseFloat(value).toFixed(2);
	};
	// 点击复制文本
	const copyText = (text) => {
		return new Promise((resolve, reject) => {
			try {
				//复制
				toClipboard(text);
				//下面可以设置复制成功的提示框等操作
				ElMessage.success('复制成功！');
				resolve(text);
			} catch (e) {
				//复制失败
				ElMessage.error('复制失败！');
				reject(e);
			}
		});
	};

	const getCurrencySymbolText = (params) => {
		let text = '';
		switch (params) {
			case CN:
				text = '￥';
				break;
			case GB:
				text = '￡';
				break;
			case US:
				text = '$';
				break;
			case DE:
				text = '€';
				break;
			default:
				text = '';
				break;
		}
		return text;
	};

	const getPriceByField = (params, completeString, cuttingStrig = 'Price') => {
		let text = '';
		let cutString = completeString.split(cuttingStrig) ? completeString.split(cuttingStrig)[0] : '';
		// 这里拿到的cutString为eu，uk，us
		switch (cutString) {
			case 'eu':
				text = getCurrencySymbolText(DE);
				break;
			case 'uk':
				text = getCurrencySymbolText(GB);
				break;
			case 'us':
				text = getCurrencySymbolText(US);
				break;

			default:
				text = '';
				break;
		}
		return params ? text + params : text + '0';
	};

	// 根据站点返回对应的国家
	const returnCountryByPlatform = (params) => {
		let state = '';
		switch (params) {
			case SITE_GB:
				state = GB;
				break;
			case SITE_US:
				state = US;
				break;
			case SITE_DE:
				state = DE;
				break;
			default:
				state = '';
				break;
		}
		return state;
	};

	// 获取国家货币字符
	const getCurrencySymbolTextBySite = (params) => {
		let platform = returnCountryByPlatform(params);
		let text = '';
		text = getCurrencySymbolText(platform);
		return text;
	};

	// 获取国家文本
	const getCountryText = (params) => {
		let text = '';
		switch (params) {
			case COUNTRY_GERMANY:
				text = '德国';
				break;
			case COUNTRY_BRITAIN:
				text = '英国';
				break;
			case COUNTRY_USA:
				text = '美国';
				break;
			default:
				text = '';
				break;
		}
		return text;
	};
	// 获取支付文本
	const getPayTypeText = (params) => {
		let text = '';
		switch (params) {
			case PAYMENT_PAYPAL:
				text = 'paypal';
				break;
			case PAYMENT_AFTERPAY:
				text = 'afterpay';
				break;
			case PAYMENT_KLARNA:
				text = 'klarna';
				break;
			case PAYMENT_CREDIT_CARD:
				text = '信用卡';
				break;
			default:
				text = '';
				break;
		}
		return text;
	};

	// 获取付款状态
	const getStatusText = (params) => {
		let text = '';
		params = params + '';
		switch (params) {
			case PENDING_PAYMENT:
				text = '待付款';
				break;
			case TO_BE_SHIPPED:
				text = '待发货';
				break;
			case SHIPPED:
				text = '已发货';
				break;
			case COMPLETED:
				text = '已完成';
				break;
			case CLOSED:
				text = '已关闭';
				break;
			case CANCELLED:
				text = '已取消';
				break;
			case INVALID_ORDER:
				text = '无效订单';
				break;
			default:
				text = '';
				break;
		}
		return text;
	};

	// 根据站点标识来返回对应的站点
	const getSiteByPlatform = (params) => {
		let text = '';
		switch (params) {
			case SITE_GB:
				text = '英国';
				break;
			case SITE_US:
				text = '美国';
				break;
			case SITE_DE:
				text = '德国';
				break;
			default:
				text = '';
				break;
		}
		return text;
	};

	// 全局的匹配国家id对应名字的方法
	const getCityNameByShorthand = (shorthand) => {
		let list = storeCityList.value;
		let name = '';
		for (let index = 0; index < list.length; index++) {
			const element = list[index];
			if (element.shorthand === shorthand) {
				name = element.countriesRegions;
			}
		}
		return name + '';
	};

	return {
		COUNTRY_GERMANY,
		COUNTRY_BRITAIN,
		COUNTRY_USA,
		percentFormat,
		dateFormatYMD,
		dateFormatYMDHMS,
		dateFormatHMS,
		scaleFormat,
		scale2Format,
		copyText,
		getPriceByField,
		getCurrencySymbolText,
		getCurrencySymbolTextBySite,
		returnCountryByPlatform,
		getCountryText,
		getPayTypeText,
		getStatusText,
		getSiteByPlatform,
		getCityNameByShorthand,
	};
}
